package client_package;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.KeyEvent;

import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.JCheckBox;


public class VueConnexion extends Vue {
	private static final long serialVersionUID = 1L;

	private JPanel contentPane;
	private JTextField textFieldLogin;
	private JPasswordField passwordField;
	private JCheckBox chckbxRetenirMonLogin;
	
	public VueConnexion(Controleur c, boolean retenirLogin, String login) {
		
		super(c);
		setResizable(false);
		setBounds(100, 100, 280, 179);
		setTitle("Connexion");

		this.addWindowListener (new WindowAdapter() {
			public void windowClosing (WindowEvent e) {
				getControleur().annuler(VueConnexion.this);
				getControleur().getOut().println("exit");
				getControleur().getOut().flush();
			}
		});
		
		
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		

		
		
		textFieldLogin = new JTextField(login);
		textFieldLogin.setBounds(113, 13, 114, 19);
		contentPane.add(textFieldLogin);
		textFieldLogin.setColumns(10);
		textFieldLogin.addKeyListener( new KeyListener(){
				public void keyPressed(KeyEvent e){
					if (e.getKeyCode() == KeyEvent.VK_ENTER){
						String login = textFieldLogin.getText();
						char[] mdp = passwordField.getPassword();
						getControleur().connexion(login, mdp, chckbxRetenirMonLogin.isSelected());
					} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
						getControleur().annuler(VueConnexion.this);
						getControleur().getOut().println("exit");
						getControleur().getOut().flush();
					}
				}
				public void keyTyped(KeyEvent e) { }
				public void keyReleased(KeyEvent e) { }
			}
		);
		
		JLabel lblLogin = new JLabel("Login :");
		lblLogin.setHorizontalAlignment(SwingConstants.RIGHT);
		lblLogin.setBounds(0, 15, 110, 15);
		contentPane.add(lblLogin);
		
		passwordField = new JPasswordField();
		passwordField.setBounds(113, 45, 114, 19);
		contentPane.add(passwordField);
		passwordField.addKeyListener( new KeyListener(){
				public void keyPressed(KeyEvent e){
					if (e.getKeyCode() == KeyEvent.VK_ENTER){
						String login = textFieldLogin.getText();
						char[] mdp = passwordField.getPassword();
						getControleur().connexion(login, mdp, chckbxRetenirMonLogin.isSelected());
					} else if (e.getKeyCode() == KeyEvent.VK_ESCAPE){
						getControleur().annuler(VueConnexion.this);
						getControleur().getOut().println("exit");
						getControleur().getOut().flush();
					}
				}
				public void keyTyped(KeyEvent e) { }
				public void keyReleased(KeyEvent e) { }
			}
		);
		
		JLabel lblMotDePasse = new JLabel("Mot de passe :");
		lblMotDePasse.setHorizontalAlignment(SwingConstants.RIGHT);
		lblMotDePasse.setBounds(0, 47, 110, 15);
		contentPane.add(lblMotDePasse);
		
		JButton btnAnnuler = new JButton("Annuler");
		btnAnnuler.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				getControleur().annuler(VueConnexion.this);
				getControleur().getOut().println("exit");
				getControleur().getOut().flush();
			}
		});
		btnAnnuler.setBounds(19, 113, 107, 25);
		contentPane.add(btnAnnuler);
		
		JButton btnConnexion = new JButton("Connexion");
		btnConnexion.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					String login = textFieldLogin.getText();
					char[] mdp = passwordField.getPassword();
					getControleur().connexion(login, mdp, chckbxRetenirMonLogin.isSelected());
			}
		});
		btnConnexion.setBounds(145, 113, 107, 25);
		contentPane.add(btnConnexion);
		
		
		
		chckbxRetenirMonLogin = new JCheckBox("Retenir mon login");
		chckbxRetenirMonLogin.setSelected(retenirLogin);
		chckbxRetenirMonLogin.setBounds(64, 77, 143, 23);
		contentPane.add(chckbxRetenirMonLogin);
		
	}
}
